package UILayer;
import java.util.Scanner;

import CtrLayer.StaffCtr;
import ModelLayer.CurrentAccount;


/**
 * Write a description of class EmployeeUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StaffUI
{
    private StaffCtr staff_ctr;
  
    /**
     * Constructor for objects of class EmployeeUI
     */
    public StaffUI()
    {
        staff_ctr = new StaffCtr();
    }
    
    
    /**********************************************MENUS**********************************************************/
    
    
    public void start_staff_menu()
    {
        boolean exit = false;
        if (!(CurrentAccount.give_rank().equals("employee")))
        {
            while (!(exit))
            {
                int choice = write_staff_menu();
                switch(choice)
                {
                    case 1 :    manage_my_account();
                                break;
                    case 2 :    add_account();
                                break;
                    case 3 :    edit_account();
                                break;
                    case 4 :    delete_account();
                                break;
                    case 5 :    find_employee();
                                break;
                    case 6 :    view_accounts();
                                break;
                    default:    exit = true;                                
                                break;
                }
            }
        }
        else
        {
            manage_my_account();
        }
        staff_ctr.save_data();
    }
    
    private void manage_my_account()
    {
        boolean exit = false;
        while(!(exit))
        {
            int choice = write_my_account_menu();
            switch(choice)
            {
                case 1 :    view_my_account();
                            break;
                case 2 :    change_my_data();
                            break;
                case 3 :    change_my_login();
                            break;
                case 4 :    change_my_password();
                            break;
                default:    exit = true;
                            break;
            }

        }
        System.out.println("\f");           // Needs checking if necessary
    }
    
    private void edit_account_menu(String id)
    {
        boolean exit = false;
        boolean is_owner = CurrentAccount.give_rank().equals("owner");
        while(!(exit))
        {
            int choice = write_edit_account_menu(is_owner);
            if (is_owner)
            {
                
                switch(choice)
                {
                    case 1 :    edit_name(id);
                                break;
                    case 2 :    edit_phone_nr(id);
                                break;
                    case 3 :    edit_address(id);
                                break;
                    case 4 :    edit_salary(id);                            
                                break;
                    case 5 :    edit_login(id);
                                break;
                    case 6 :    edit_password(id);
                                break;
                    case 7 :    edit_is_manager(id);
                                break;
                    default:    exit = true;
                                break;
                }
            }
            else
            {
                switch(choice)
                {
                    case 1 :    edit_name(id);
                                break;
                    case 2 :    edit_phone_nr(id);
                                break;
                    case 3 :    edit_address(id);
                                break;
                    case 4 :    edit_salary(id);
                                break;
                    default:    exit= true;
                                break;
                }
            }
                    
        }
    }
    
    /*********************************************************MENU WRITERS*******************************************************/
    
    private int write_edit_account_menu(boolean owner)
    {
        System.out.println("\f***EDIT ACCOUNT***");
        System.out.println("Press (1) to change name.");
        System.out.println("Press (2) to change phone number");
        System.out.println("Press (3) to change address.");
        System.out.println("Press (4) to change salary.");
        if (owner)
        {
            System.out.println("Press (5) to change login.");
            System.out.println("Press (6) to change password.");
            System.out.println("Press (7) edit whether the employee is a manager or not.");
        }
        System.out.println("Press any other number to exit.");
        return int_input();
    }
        
    
    private int write_staff_menu()
    {
        System.out.println("\f***ACCOUNT MENU***");
        System.out.println("Press (1) to manage your account.");
        System.out.println("Press (2) to add new emloyee");
        System.out.println("Press (3) to edit existing employee account.");
        System.out.println("Press (4) to delete an employee.");
        System.out.println("Press (5) to view an account of desired employee.");
        System.out.println("Press (6) to view all employees' accounts.");
        System.out.println("Press any other number to exit.");
        return int_input();
    }
    
    private int write_my_account_menu()
    {
        System.out.println("\f***MY ACCOUNT***");
        System.out.println("Press (1) to view your data.");
        System.out.println("Press (2) to edit your data.");
        System.out.println("Press (3) to change your login.");
        System.out.println("Press (4) to change your password.");
        System.out.println("Press any other number to exit.");
        return int_input();
    }
       
    
    /****************************************************INPUT READERS****************************************************/
    
    private int int_input()
    {
        int input = 0;
        boolean ok = false;
        Scanner keyboard = new Scanner(System.in);
        while(!(ok))
        {
            try
            {
                input = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must provide a valid input.\nYou have to type a number");
                keyboard.nextLine();
            }
        }
        return input;
    }
    
    private String string_input()
    {
        Scanner keyboard = new Scanner(System.in);
        String output;
        while((output = keyboard.nextLine()).equals(""))
        {
            
        }
        return output;
    }
    
    private String input_id()
    {
        System.out.println("Please write the ID of the employee or write (0) to return.");
        String id = "" + int_input();
        return  id;
    }
    
    private String input_name()
    {
        System.out.println("Please write the name:");
        return string_input();
    }
    
    private String input_phone_nr()
    {
        String phone = "";
        String new_phone;
        System.out.println("Please write phone number");
        boolean wrong = true;
        while (wrong)
        {            
             phone = string_input();
             if(phone.matches("\\+?[0-9]+(:?-[0-9]+)*"))
             {
                 if (phone.contains("-"))
                 {
                     new_phone = phone.replaceAll("-","");
                 }
                 else
                 {
                     new_phone = phone;
                 }
                 
                 if((new_phone.length() == 8) || (new_phone.length() == 11 && new_phone.startsWith("+")))
                 {
                     wrong = false;
                 }
                 else
                 {
                     System.out.println("The phone was typed incorrectly, type it again:");
                 }
             }
             else
             {
                 System.out.println("The phone was typed incorrectly, type it again:");
             }
        }
        return phone;
    }
    
    private String input_address()
    {
        System.out.println("Please write the address:");
        return string_input();
    }
    
    private String input_postal_code()
    {
        String code = "";
        boolean wrong = true;
        System.out.println("Please write postal code:");
        while(wrong)
        {
            code = string_input();
            if(code.matches("[0-9]+") && code.length() == 4)             // Fix this one, so it chcecks for first two alphanumericals.
            {
                wrong = false;
            }
            else
            {
                System.out.println("The postal code was typed incorrectly, type it again:");
            }
        }
        return code;
    }
    
    private String input_city()
    {
        System.out.println("Please write the city:");
        return string_input();
    }
    
    private String input_login()
    {
        System.out.println("Please write employee's login");
        String login;
        while(staff_ctr.is_login_used(login = string_input()))
        {
            System.out.println("This login is already taken. Choose another one.\n");
        }
        return login;
    }
    
    private String input_new_login()
    {
        System.out.println("Please write your new login:");
        String login;
        while(staff_ctr.is_login_used(login = string_input()))
        {
            System.out.println("This login is already taken. Choose another one.\n");
        }
        return login;
    }

    private String input_password()
    {
        System.out.println("Please write your password or write (0) to return.");
        return string_input();
    }
    
    private String input_new_password()
    {
        String new_password = "";
        boolean wrong = true;
        while(wrong)
        {
            System.out.println("Please write a new password");
            new_password = string_input();
            System.out.println("Please write a new password again");
            if(new_password.equals(string_input()))
            {
                wrong = false;
            }
            else
            {
                System.out.println("Password was not changed.\nFirst and second entry of the password do not match.\nPlease, try again.\n");
                wait_for_input();
            }
        }
        return new_password;
    }
    
    private boolean input_is_manager()
    {
        String is_he;
        System.out.println("Enter (y) if the employee is a manager and (n) if he isn't:");
        while(true)
        {
            is_he = string_input();
            if(is_he.equals("y"))
            {
                return true;
            }
            else if (is_he.equals("n"))
            {
                return false;
            }
            else
            {
                System.out.println("Incorrect, please write (y) if the employee is a manager or (n) if he is not:");
            }
        }
    }
    
    private int input_salary()
    {
        System.out.println("Please write the salary");
        return int_input();
    }

        
    
    /****************************************************MENU FUNCTIONS****************************************************/
    
    
    
    private void view_my_account()
    {
        System.out.println(CurrentAccount.view_account());
        wait_for_input();
    }
    
    private void change_my_data()
    {
        String name = input_name();
        String phone_nr = input_phone_nr();
        String address = input_address();
        String postal_code = input_postal_code();
        String city = input_city();
        CurrentAccount.change_account_data(name, phone_nr, address, postal_code, city);
        System.out.println("Data successfully changed.");
        wait_for_input();
    }
    
    private void change_my_login()
    {
        boolean wrong = true;
        while(wrong)
        {
            String password = input_password();
            if(password.equals("0"))
            {
                break;
            }
            else if(CurrentAccount.check_password(password))
            {
                String login;
                while(staff_ctr.is_login_used(login = input_new_login()))
                {
                    System.out.println("This login is currently used by another person. Choose another one.");
                }
                CurrentAccount.change_login(login);
                wrong = false;
                System.out.println("Login was changed successfully!");
                wait_for_input();
            }
            else
            {
                System.out.println("Your password is incorrect, try again");
            }
        }
    }
    
    private void change_my_password()
    {
        boolean wrong = true;
        while(wrong)
        {
            String password = input_password();
            if(password.equals("0"))
            {
                break;
            }
            else if(CurrentAccount.check_password(password))
            {
                CurrentAccount.change_password(input_new_password());
                wrong = false;
                System.out.println("Password was changed successfully!");
                wait_for_input();
            }
            else
            {
                System.out.println("Your password is incorrect, try again");
            }
        }
            
    }
    
    private void add_account()
    {
        String name = input_name();
        String phone_nr = input_phone_nr();
        String address = input_address();
        String postal_code = input_postal_code();
        String city = input_city();
        String login;
        while(staff_ctr.is_login_used(login = input_login()))
        {
            System.out.println("This login is currently used by another person. Choose another one.");
        }
        String password = input_new_password();
        int salary = input_salary();
        boolean is_manager = false;
        
        if (CurrentAccount.give_rank().equals("owner"))
        {
            is_manager = input_is_manager();
        }
        staff_ctr.add_account(name, phone_nr, address, postal_code, city, login, password, salary, is_manager);
    }
    
    private void edit_account()
    {
        String id = "";
        boolean ok = false;
        while(!(ok))
        {
            if(staff_ctr.is_such_id(id = input_id()))
            {
                if(id.equals("0"))
                {
                    break;
                }
                if(staff_ctr.is_manager(id) && CurrentAccount.give_rank().equals("manager"))
                {
                    System.out.println("You are not allowed to modify account of this person.\n");
                }
                else
                {
                    ok = true;
                    edit_account_menu(id);
                }
            }
            else
            {
                System.out.println("No such employee.\n");
            }
        }
        
    }
    
    private void delete_account()
    {
        boolean wrong = true;
        while(wrong)
        {
            String id = look_for_employee();
            if (id.equals("0"))
            {
                break;
            }
            else if(CurrentAccount.give_rank().equals("manager") && staff_ctr.is_manager(id))
            {
                System.out.println("You are not allowed to perform this operation.");
            }
            else
            {
                wrong = false;
                if(staff_ctr.delete_account(id))
                {
                    System.out.println("The account was successfully removed from the system");
                }
                else
                {
                    System.out.println("The account was not removed from the system.");
                }
                wait_for_input();
            }
        }
        
    }
    
    private void find_employee()
    {
        String id = look_for_employee();
        if(!(id.equals("0")))
        {
            System.out.println(staff_ctr.print_employee(id));
            wait_for_input();
        }
    }
    
    private void view_accounts()
    {
        System.out.println(staff_ctr.print_all_employees());
        wait_for_input();
    }
    /****************************************************EDIT ACCOUNT FUNCTIONS****************************************************/
    
    private void edit_name(String id)
    {
        staff_ctr.edit_name(id, input_name());
    }
    
    private void edit_phone_nr(String id)
    {
        staff_ctr.edit_phone_nr(id, input_phone_nr());
    }
    
    private void edit_address(String id)
    {
        String address = input_address();
        String postal_code = input_postal_code();
        String city = input_city();
        staff_ctr.edit_address(id, address, postal_code, city);
    }
    
    private void edit_salary(String id)
    {
        staff_ctr.edit_salary(id, input_salary());
    }
    
    private void edit_login(String id)
    {
        String login;
        while(staff_ctr.is_login_used(login = input_login()))
        {
            System.out.println("This login is currently used by another person. Choose another one.");
        }
        staff_ctr.edit_login(id, login);
    }
    
    private void edit_password(String id)
    {
        staff_ctr.edit_password(id, input_new_password());
    }
    
    private void edit_is_manager(String id)
    {
        staff_ctr.edit_is_manager(id, input_is_manager());
    }
    /****************************************************OTHER FUNCTIONS****************************************************/
    
    private void wait_for_input()
    {
        System.out.println("\nPRESS ENTER TO CONTINUE\n");
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private String look_for_employee()
    {
        String id;
        while(!(staff_ctr.is_such_id(id = input_id())))
        {
            System.out.println("There is no employee with this ID");
        }
        return id;
    }
    
    

}
